<?php

namespace App\Observers;

use App\Model\BistroIncomeDay;
use App\Model\BistroIncomeMonth;
use App\Model\OrderProduct;
use App\Model\Order;
use App\Model\Product;
use App\Model\ProductSaleDay;
use App\Model\ProductSaleMonth;
use App\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class OrderObserver
{
    /**
     * 监听订单指派
     */
//    public function created(DeliveryNote $delivery_note)
//    {
//        $order = Order::findOrFail($delivery_note->order_id);
//        $packing_man = BackendUser::findOrFail($delivery_note->packing_man_id);
//        $delivery_man = BackendUser::findOrFail($delivery_note->deliveryman_id);
//        $logdata['user_id'] = $delivery_note->operation_man_id;
//        $logdata['user_type'] = '2';
//        $logdata['log_type']='2';//创建加盟商
//        $logdata['summary'] = '指派了任务';
//        $logdata['content']='用户成功指派'.$packing_man->name.'分发'.$order->order_on.'至送货人'.$delivery_man->name;
//        Log::create($logdata);
//    }

    public function updated(Order $order)
    {
//        //触发统计
//
//        if($order->status == 'payed' && $order->order_status == 'ordered') {
//            DB::beginTransaction();
//            try {
//                    $date = date('Y-m-d');
//                    $month = date('Y-m');
//                    $year = date('Y');
//
//                    //查询订单中商品 是否有代金金额 有 就添加用户代金金额
//                    $order_products = OrderProduct::where('order_id', $order->id)->get();
//                    $total_amount = 0;
//
//                    foreach ($order_products as $op) {
//                        $product = Product::findorfail($op->product_id);
//                        $product->sale_count = $product->sale_count * 1 + $op->num * 1;
//                        $product->save();
//
//                        //商品日销售
//                        $product_sale_day = ProductSaleDay::date($date)->productId($op->product_id)->first();
//                        if(empty($product_sale_day)) {
//                            $product_sale_day = new ProductSaleDay();
//                            $product_sale_day->product_id = $op->product_id;
//                            $product_sale_day->sale_count = 0;
//                            $product_sale_day->total_amount = 0;
//                            $product_sale_day->date = $date;
//                            $product_sale_day->product_name = $product->name;
//                        }
//                        $product_sale_day->sale_count = $product_sale_day->sale_count * 1 + $op->num * 1;
//                        $product_sale_day->total_amount = $product_sale_day->total_amount * 1 + $op->total_amount * 1;
//                        $product_sale_day->save();
//
//                        //商品月销售
//                        $product_sale_month = ProductSaleMonth::month($month)->productId($op->product_id)->first();
//                        if(empty($product_sale_month)) {
//                            $product_sale_month = new ProductSaleMonth();
//                            $product_sale_month->product_id = $op->product_id;
//                            $product_sale_month->sale_count = 0;
//                            $product_sale_month->total_amount = 0;
//                            $product_sale_month->month = $month;
//                            $product_sale_month->year = $year;
//                            $product_sale_month->product_name = $product->name;
//                        }
//                        $product_sale_month->sale_count = $product_sale_month->sale_count * 1 + $op->num * 1;
//                        $product_sale_month->total_amount = $product_sale_month->total_amount * 1 + $op->total_amount * 1;
//                        $product_sale_month->save();
//                        $total_amount += $op->total_amount;
//                    }
//
//                    //店铺日收益计算
//                    $bistro_income_day = BistroIncomeDay::date($date)->bistroId($order->bistro_id)->first();
//                    if(empty($bistro_income_day)) {
//                        $bistro_income_day = new BistroIncomeDay();
//                        $bistro_income_day->bistro_id = $order->bistro_id;
//                        $bistro_income_day->date = $date;
//                        $bistro_income_day->income = 0;
//                        $bistro_income_day->outcome = 0;
//                    }
//                    $bistro_income_day->income = $bistro_income_day->income * 1 + $total_amount * 1;
//                    $bistro_income_day->save();
//
//                    //店铺月收益计算
//                    $bistro_income_month = BistroIncomeMonth::month($month)->bistroId($order->bistro_id)->first();
//                    if(empty($bistro_income_month)) {
//                        $bistro_income_month = new BistroIncomeMonth();
//                        $bistro_income_month->bistro_id = $order->bistro_id;
//                        $bistro_income_month->month = $month;
//                        $bistro_income_month->year = $year;
//                        $bistro_income_month->income = 0;
//                        $bistro_income_month->outcome = 0;
//                    }
//                    $bistro_income_month->income = $bistro_income_month->income * 1 + $total_amount * 1;
//                    $bistro_income_month->save();
//                    $user = User::findorfail($order->user_id);
//
//                    //如果这个桌子上请客的直接删掉请客单
//                    $now_open_id_str = Redis::get('orderdinner:position_id||'.$order->position_id);
//                    if(empty($now_open_id_str)) {
//                        $now_open_id_s = [];
//                    }else{
//                        $now_open_id_s = json_decode($now_open_id_str, true);
//                    }
//                    if(in_array($user->open_id, $now_open_id_s)) {
//                        $key = array_search($user->open_id, $now_open_id_s);
//                        unset($now_open_id_s[$key]);
//                        $now_open_id_sstr = json_decode($now_open_id_s);
//                        Redis::setex('orderdinner:position_id||'.$order->position_id, $now_open_id_sstr);//付款成功，请客结束
//                    }
//
//                DB::commit();
//            } catch (\Exception $e) {
//                DB::rollback();
//                \Log::info($e->getMessage());
////                return $this->apiError($e->getMessage());
//            }
//        }

//        if($order->status != 'ordered' && $order->status != 'shiped_not_pay'){
//            return;
//        }
//
//        //上菜
//        if($order->order_status == 'ordered') {
//            //上菜未付款
//            $order_products = OrderProduct::where('order_id', $order->id)->get();
//            DB::beginTransaction();
//            try {
//                foreach ($order_products as $od) {
//                    if($od->status != 'done'){
//                        $od->status = 'done';
//                        $od->serve_num = $od->num + $od->return_num;//数量 + 退回数量
//                        $od->save();
//                    }
//                }
//
//                DB::commit();
//            } catch (\Exception $e) {
//                DB::rollback();
//                \Log::info($e->getMessage());
////            return $this->apiError($e->getMessage());
//            }
//        }
    }

}